<template>
  <view class="content">
    <!-- 顶部轮播图 -->
    <up-swiper :list="bannerList" indicator indicatorMode="dot" circular height="180" radius="0"></up-swiper>

    <!-- 热门供需速览 -->
    <view class="section">
      <view class="section-header">
        <text class="section-title">热门供需速览</text>
        <text class="section-more" @click="toDemands">查看更多</text>
      </view>
      <view class="demand-list">
        <view class="demand-item" v-for="item in hotDemands" :key="item.id" @click="toDetail(item)"  >
          <view class="demand-tag" :class="item.type == 1 ? 'tag-green' : 'tag-orange'">{{ getDemandsLabel(item.type) }}</view>
          <view>
            <view class="demand-title">{{ item.content }}</view>
            <view class="demand-info">
              <text>{{ item.updateTime }}</text>
              <text class="demand-view">👁 {{ item.viewCount }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 会员风采 -->
    <view class="section" style="background: none;">
      <view class="section-header">
        <text class="section-title">会员风采</text>
        <!-- <text class="section-more">查看全部</text> -->
      </view>
      <view scroll-x class="member-list">
        <view class="member-item" v-for="item in members" :key="item.id">
          <image :src="imgUrl+item.logoUrl" class="member-avatar" />
          <view class="member-role" :class="item.memberLevelId == 0 ? 'role-red' : 'role-green'">{{ getEmLabel(item.memberLevelId) }}</view>
          <view class="member-name">{{ item.enterpriseName }}</view>
          <view class="member-desc">{{ item.desc }}</view>
        </view>
      </view>
    </view>

    <!-- 协会简介 -->
    <view class="section">
      <view class="intro-header">
        <text class="intro-title">协会介绍</text>
      </view>
      <view class="intro-content">
        {{introduction}}
        <!-- <text class="intro-more" @click="toEnterpriseDetail()">了解详情</text> -->
      </view>
    </view>

    <!-- 行业资讯 -->
    <view class="section">
      <view class="section-header">
        <text class="section-title">行业资讯</text>
      </view>
      <view class="news-tabs">
        <text v-for="(tab, idx) in newsType" @click="changeTab(tab,idx)" :key="tab" :class="['news-tab', chooseIdx === idx ? 'active' : '']">{{ tab.dictLabel
          }}</text>
      </view>
      <view class="news-list">
        <view class="news-item" v-for="item in newsList" :key="item.id" @click="toNews(item)" >
          <image :src="imgUrl+item.coverImageUrl" class="news-img" />
          <view class="news-info">
            <view class="news-title">{{ item.title }}</view>
            <view class="news-meta">
              <text>{{ item.updateTime }}</text>
              <text class="news-view">👁 {{ item.viewCount }}</text>
            </view>
          </view>
        </view>
        <view v-if="newsList.length==0 " style="margin-top: 20rpx;" >
            <up-empty
                mode="data"
                text="暂无数据"
            >
            </up-empty>
        </view>
      </view>
    </view>

    <tabbar :selected="0" />
  </view>
</template>

<script setup>
import { ref,onMounted } from 'vue'
import tabbar from '@/components/tabbar.vue'
import {imgUrl} from '@/config/index.js'
import {getHotData,getListHome,getRankInfo,getDict,getAssociationInfo} from '@/api/index.js'
import {onShow} from '@dcloudio/uni-app'
onShow(() => {
  uni.hideTabBar()

  getHotData().then(res=>{
    hotDemands.value=res.data
  })
  getListHome().then(res=>{
    if(res.code==200){
      members.value=res.data
    }
  })
  getRankInfo().then(res=>{
    newsList.value=res.data
  })
})
onMounted(() => {
  getDict('demand_type').then(res => {
    demandsType.value = res.data
  })
  getDict('em_type').then(res => {
    emType.value = res.data
  })
  getDict('news_type').then(res => {
    newsType.value = res.data
    newsType.value.unshift({ dictValue: '', dictLabel: '全部' })
  })
  let carouselImageUrls =uni.getStorageSync('associationInfo')?uni.getStorageSync('associationInfo').carouselImageUrls:[]
  introduction.value=uni.getStorageSync('associationInfo')?uni.getStorageSync('associationInfo').introduction:''
  bannerList.value=carouselImageUrls.split(',').map(item=>{
    return imgUrl+item
  })
  
  // getAssociationInfo().then(res => {
  //   bannerList.value=JSON.parse(res.data.carouselImageUrls)
  //   introduction.value=res.data.introduction
  // }) 
})
const toDemands=()=>{
  uni.switchTab({
    url:'/pages/supply/index',
  })
}
const toDetail = (item) => {
  uni.navigateTo({ url: `/pages/supply/detail?id=${item.id}` })
}
const toNews=(item)=>{
  uni.navigateTo({
    url:'/pages/index/newsDetail?id='+item.id,
  }) 
}
const changeTab=(tab,index)=>{ 
  chooseIdx.value=index
  
  getRankInfo({type:tab.dictValue}).then(res=>{
    newsList.value=res.data
  })
}
// 轮播图数据
const bannerList = ref([])
let introduction=ref('')

// 热门供需速览
const hotDemands = ref([])
let demandsType=ref([])
const getDemandsLabel=(type)=>{ 
  let index=demandsType.value.findIndex(i=>i.dictValue==type)
  return index>-1?demandsType.value[index].dictLabel:''
}

// 会员风采
const members = ref([])
let emType=ref([])
const getEmLabel=(type)=>{ 
  let index=emType.value.findIndex(i=>i.dictValue==type)
  return index>-1?emType.value[index].dictLabel:''
}

// 行业资讯
// const newsTabs = ref(['供需动态', '政策法规', '市场分析', '成功案例', '行业新闻'])
let newsType=ref([])
const newsList = ref([
  { id: 1, img: 'https://picsum.photos/200/200?random=20', title: '2023年度春季供需对接会成功举办，达成合作意向32项', date: '2023-04-15', views: 528 },
  { id: 2, img: 'https://picsum.photos/200/200?random=21', title: '2023年二季度原材料价格波动分析及对供需市场影响', date: '2023-04-10', views: 342 }
])
let chooseIdx=ref(0)
const toEnterpriseDetail = () => {
  uni.navigateTo({
    url: '/pages/mine/enterprise_detail'
  })
}
</script>

<style scoped>
.content {
  background: #f7f8fa;
  min-height: 100vh;
  padding-bottom: 120rpx;
  box-sizing: border-box;
}
.section {
  background: #fff;
  margin: 16px 0;
  border-radius: 12px;
  padding: 16px;
}
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.section-title {
  font-weight: bold;
  font-size: 18px;
}
.section-more {
  color: #409eff;
  font-size: 14px;
}
.demand-list {
  margin-bottom: 8px;
}
.demand-item {
  display: flex;
  align-items: center;
  background: #f5f7fa;
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 8px;
}
.demand-tag {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 12px;
  margin-right: 8px;
  white-space: nowrap;
}
.tag-green {
  background: #e6f9ea;
  color: #2ecc71;
}
.tag-orange {
  background: #fff3e6;
  color: #faad14;
}
.demand-title {
  flex: 1;
  font-size: 15px;
  color: #333;
}
.demand-info {
  font-size: 12px;
  color: #999;
  margin-left: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: space-between;
}
.demand-view {
  margin-left: 4px;
}
.demand-more {
  color: #409eff;
  font-size: 14px;
  text-align: center;
  margin-top: 6px;
}
.member-list {
  display: flex;
  flex-direction: row;
  overflow-x: auto;
  /* gap: 12px; */
  padding-bottom: 8px;
  width: 100%;
}
.member-item {
  width: 90px;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex-shrink: 0;
  text-align: center;
  margin-right: 12px;
  flex-wrap: nowrap;
  background-color: #fff;
  padding: 12rpx;
  border-radius: 20rpx;
}
.member-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin-bottom: 4px;
}
.member-role {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  display: inline-block;
  margin-bottom: 2px;
}
.role-red {
  background: #ffeaea;
  color: #fa5151;
}
.role-green {
  background: #e6f9ea;
  color: #2ecc71;
}
.member-name {
  font-size:24rpx;
  font-weight: bold;
  margin: 10rpx 0;
  white-space: nowrap;
  overflow-x: scroll;
  /* text-overflow: ellipsis; */
  width: 180rpx;
}
.member-desc {
  font-size: 24rpx;
  color: #999;
}
.intro-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
.intro-title {
  font-weight: bold;
  font-size: 16px;
  border-left: 4px solid #409eff;
  padding-left: 8px;
}
.intro-content {
  font-size: 14px;
  color: #333;
  line-height: 1.6;
}
.intro-more {
  color: #409eff;
  margin-left: 8px;
  font-size: 14px;
}
.news-tabs {
  display: flex;
  gap: 10px;
  width: 100%;
  overflow-x: scroll;
  margin-bottom: 10px;

}
.news-tab {
  font-size: 14px;
  color: #666;
  padding: 4px 12px;
  border-radius: 16px;
  background: #f5f7fa;
  white-space: nowrap;
}
.news-tab.active {
  background: #409eff;
  color: #fff;
}
.news-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.news-item {
  display: flex;
  gap: 10px;
  background: #f5f7fa;
  border-radius: 8px;
  padding: 8px;
}
.news-img {
  width: 60px;
  height: 60px;
  border-radius: 8px;
  object-fit: cover;
}
.news-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.news-title {
  font-size: 15px;
  font-weight: bold;
  color: #333;
}
.news-meta {
  font-size: 12px;
  color: #999;
  display: flex;
  gap: 10px;
  align-items: center;
}
.news-view {
  margin-left: 4px;
}
</style>